﻿using Terraria;
using Terraria.GameContent.Bestiary;
using Terraria.GameContent.ItemDropRules;
using Terraria.ID;
using Terraria.ModLoader;

namespace HardcoreGame.Content.NPCs.Enemies.Biomesa.Hallow
{
    public class Bicorn : ModNPC
    {
        public override void SetStaticDefaults()
        {
            Main.npcFrameCount[NPC.type] = Main.npcFrameCount[86];
        }
        public override void ModifyNPCLoot(NPCLoot npcLoot)
        {
            npcLoot.Add(ItemDropRule.Common(526, 1, 2, 2));
        }
        public override void SetDefaults()
        {
            NPC.CloneDefaults(86);
            NPC.width = 126;
            NPC.height = 82;
            NPC.damage = 76;
            NPC.defense = 72;
            NPC.lifeMax = 3000;
            NPC.knockBackResist = 0.28f;
            NPC.HitSound = SoundID.NPCHit12;
            NPC.DeathSound = SoundID.NPCDeath18;
            NPC.value = 150f;
            NPC.aiStyle = 26;
            AIType = 86;
            AnimationType = 86;
            Banner = Item.NPCtoBanner(86);
            BannerItem = Item.BannerToItem(Banner);
        }
        public override float SpawnChance(NPCSpawnInfo spawnInfo)
        {
            if (spawnInfo.Player.ZoneHallow && spawnInfo.Player.ZoneOverworldHeight && !spawnInfo.Player.ZoneTowerStardust && !spawnInfo.Player.ZoneTowerVortex && !spawnInfo.Player.ZoneTowerNebula && !spawnInfo.Player.ZoneTowerSolar)
            {
                return 0.25f;
            }
            if (spawnInfo.Player.ZoneHallow && spawnInfo.Player.ZoneOverworldHeight && !spawnInfo.Player.ZoneTowerStardust && !spawnInfo.Player.ZoneTowerVortex && !spawnInfo.Player.ZoneTowerNebula && !spawnInfo.Player.ZoneTowerSolar)
            {
                return 0.35f;
            }
            return 0f;
        }
        public override void SetBestiary(BestiaryDatabase database, BestiaryEntry bestiaryEntry)
        {
            bestiaryEntry.Info.AddRange([
                BestiaryDatabaseNPCsPopulator.CommonTags.SpawnConditions.Biomes.TheHallow,
                new FlavorTextBestiaryInfoElement("Mods.HardcoreGame.Bestiary.Bicorn")
            ]);
        }
        public override void HitEffect(NPC.HitInfo hit)
        {
            for (int i = 0; i < 10; i++)
            {
                int dustType = 5;
                int dustIndex = Dust.NewDust(NPC.Center, NPC.width, NPC.height, dustType, 0f, 0f, 0, default, 1f);
                Dust dust = Main.dust[dustIndex];
                dust.velocity.X += Main.rand.Next(-50, 51) * 0.01f;
                dust.velocity.Y += Main.rand.Next(-50, 51) * 0.01f;
                dust.scale *= 1f + Main.rand.Next(-30, 31) * 0.01f;
            }
            if (NPC.life <= 0)
            {
                for (int j = 0; j < 6; j++)
                {
                    int dust2 = Dust.NewDust(NPC.Center, NPC.width, NPC.height, DustID.Blood, 2 * hit.HitDirection, -2f, 0, default, 1f);
                    if (Main.rand.NextBool(2))
                    {
                        Main.dust[dust2].noGravity = true;
                        Main.dust[dust2].scale = 1.2f * NPC.scale;
                    }
                    else
                    {
                        Main.dust[dust2].scale = 0.7f * NPC.scale;
                    }
                }
                var entitySource = NPC.GetSource_Death();
                Gore.NewGore(entitySource, NPC.position, NPC.velocity, Mod.Find<ModGore>("Gore_21").Type, 1f);
                Gore.NewGore(entitySource, NPC.position, NPC.velocity, Mod.Find<ModGore>("Gore_22").Type, 1f);
                Gore.NewGore(entitySource, NPC.position, NPC.velocity, Mod.Find<ModGore>("Gore_23").Type, 1f);
            }
        }
    }
}

